#!/bin/bash

SSH_OPTS="-F ../deploy/ssh_config"

pcs_log=/mnt/pcslog/fxplatform.log

worker_log=/mnt/pcslog/fxworker.log

cmis_log=/usr/share/tomcat/debug.log

cas_log=/mnt/pcslog/fxcas.log

help(){
    echo -e "[帮助信息]\t\t\t\t"
    echo -e "[使用方法]:\t\t\t\t p1_utils [arg]"
    echo -e "p1_utils -clear_download_cache\t\t 清空下载地址的缓存"
    echo -e "p1_utils -check_server_status\t\t 查看所有服务的状态"
    echo -e "p1_utils -show_server_error\t\t 查看所有服务的错误日志"
    echo -e "p1_utils -deploy_cmis\t\t 部署产品cmis服务"
    echo -e "p1_utils -deploy_pcs\t\t 部署产品pcs服务"
    echo -e "p1_utils -deploy_cas\t\t 部署产品cas服务"
    echo -e "p1_utils -deploy_worker\t\t 部署产品worker服务"
exit 1
}

checkServerStatus(){
    echo 'checkServerStatus'
    echo "------View CMIS Service Process"
    count=`netstat -nat | grep 8180 | wc -l`
 if [ $count -eq 0 ]; then
    echo "CMIS Process Not Exist,Try Restart CMIS"
    sudo service tomcat restart
else
    echo "CMIS Service Status:OK"
 fi;

    echo "------View CAS Service Process"
    count=`netstat -nat | grep 8888 | wc -l`
 if [ $count -eq 0 ]; then
    echo "CAS Process Not Exist,Try Restart CAS"
    sudo service fxcas restart
else
    echo "CAS Service Status:OK"
 fi;

    echo "------View Platform Service Process"
    count=`netstat -nat | grep 8003 | wc -l`
 if [ $count -eq 0 ]; then
    echo "Platform Process Not Exist,Try Restart Platform"
    sudo service fxplatform restart
else
    echo "Platform Service Status:OK"
 fi;

    echo "------View Worker Service Process"
    count=`netstat -nat | grep 8002 | wc -l`
 if [ $count -eq 0 ]; then
    echo "Worker Process Not Exist,Try Restart Platform"
    sudo service fxworker restart
else
    echo "Worker Service Status:OK"
 fi;

}

show_server_error(){
    echo ${pcs_log}
    tail -50 ${pcs_log}|grep ERROR > /dev/null
  if [ $? -eq 0 ];then
    tail -30 ${pcs_log}
  else
    echo "pcs running ok"
  fi;

    echo ${cmis_log}
    tail -50 ${cmis_log}|grep ERROR > /dev/null
  if [ $? -eq 0 ];then
    tail -30 ${cmis_log}
  else
    echo "cmis running ok"   
  fi;
    echo ${cas_log}
    tail -50 ${cas_log}|grep ERROR > /dev/null
  if [ $? -eq 0 ];then
    tail -30 ${cas_log}
  else
    echo "cas running ok"   
  fi;
    echo ${worker_log}
    tail -50 ${worker_log}|grep ERROR > /dev/null
  if [ $? -eq 0 ];then
    tail -30 ${worker_log}
  else
    echo "worker running ok"   
  fi;

}


deploy_demo2(){
    echo "Beginning Deploy ops48 demo2 Env"
    ssh -p333 root@157.22.19.188 "cd /vagrant/vagrant && make deploy_pcs"
}

deploy_demo(){
    echo "Beginning Deploy ops47 demo Env"
    ssh -p333 root@157.22.19.181 "cd /vagrant/vagrant && make deploy_pcs"
}

deploy_demo_cn(){
    echo "Beginning Deploy demo-cn Env"
    ssh -p22 root@123.56.234.214 "cd /vagrant/vagrant && make deploy_pcs"
}

deploy_cmis(){
    echo "Beginning Deploy Cmis Pro Env"
    ssh $SSH_OPTS root@10.1.13.209 "cd /vagrant/vagrant && ./download_cmis.sh master && service tomcat restart && tail -f /usr/share/tomcat/debug.log"
}

deploy_pcs(){
   echo "Beginning Deploy Pcs Pro Env So files"
   ssh $SSH_OPTS root@10.1.103.58 "cd /vagrant/vagrant && ./download_files.sh master platform && service fxplatform restart && tail -f /mnt/pcslog/fxplatform.log"
}

deploy_cas(){
   echo "Beginning Deploy Cas Pro Env"
   ssh $SSH_OPTS root@10.1.13.215 "cd /vagrant/vagrant && ./download_files.sh master cas && systemctl restart fxcas && tail -f /var/log/fxcas.log"
}

deploy_worker(){
   echo "Beginning Deploy Worker Pro Env So files"
   ssh $SSH_OPTS root@10.1.103.59 "cd /vagrant/vagrant && ./download_files.sh master worker && service fxworker restart && tail -f /mnt/pcslog/fxworker.log"
}


if [ $# -gt 0 ]; then

    if [ $1 = '--help' ]; then
        help
    fi;

    if [ $1 = '-clear_download_cache' ]; then
        if [ -e /tmp/download_server.txt ]; then
            sudo rm -f /tmp/download_server.txt
        fi
        exit
    fi;

    if [ $1 = '-check_server_status' ]; then
        checkServerStatus
        exit
    fi;

    if [ $1 = '-show_server_error' ]; then
        show_server_error
        exit
    fi;

    if [ $1 = '-deploy_pcs' ]; then
        deploy_pcs
    fi;

    if [ $1 = '-deploy_cmis' ]; then
        deploy_cmis
    fi;

    if [ $1 = '-deploy_cas' ]; then
        deploy_cas
    fi;

    if [ $1 = '-deploy_worker' ]; then
        deploy_worker
    fi;

    if [ $1 = '-deploy_demo-cn' ]; then
        deploy_demo_cn
    fi;

    if [ $1 = '-deploy_demo' ]; then
        deploy_demo
    fi;

    if [ $1 = '-deploy_demo2' ]; then
        deploy_demo2
    fi;

else
    echo -e "错误的输入参数: $0 $1"
    help
    exit -1
fi
exit
